package com.lingnan.controller;


import com.lingnan.dto.CommentCreateDTO;
import com.lingnan.dto.PostCreateDTO;
import com.lingnan.entity.po.CommunityComment;
import com.lingnan.entity.po.CommunityPost;
import com.lingnan.entity.vo.Result;
import com.lingnan.service.ICommunityService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/community")
public class CommunityController {
    private final ICommunityService communityService;

    public CommunityController(ICommunityService communityService) {
        this.communityService = communityService;
    }

    /**
     * 创建帖子
     */
    @PostMapping("/posts")
    public Result createPost(@RequestBody PostCreateDTO dto) {
        CommunityPost post = new CommunityPost();
        BeanUtils.copyProperties(dto, post);
        Long postId = communityService.createPost(post);
        return Result.ok(postId);
    }

    /**
     * 删除帖子
     */
    @DeleteMapping("/posts/{postId}")
    public Result deletePost(@PathVariable Long postId) {
        return Result.ok(communityService.deletePost(postId));
    }

    /**
     * 分页查询帖子（默认时间排序）
     */
    @GetMapping("/posts")
    public Result listPosts(
            @RequestParam(name = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(name = "pageSize",defaultValue = "10") int pageSize,
            @RequestParam(name = "sortBy", defaultValue = "time_desc") String sortBy) {

        // 使用 if-else 替代 switch
        String sortParam;
        if ("like".equals(sortBy)) {
            sortParam = "like";
        } else if ("time_asc".equals(sortBy)) {
            sortParam = "time_asc";
        } else {
            sortParam = "time_desc";
        }

        return Result.ok(communityService.listPosts(pageNum, pageSize, sortParam));
    }

    /**
     * 获取帖子详情
     */
    @GetMapping("/posts/{postId}")
    public Result getPost(@PathVariable(name = "postId") Long postId) {
        return Result.ok(communityService.getPostById(postId));
    }

    /**
     * 创建评论
     */
    @PostMapping("/comments")
    public Result createComment(@RequestBody CommentCreateDTO dto) {
        CommunityComment comment = new CommunityComment();
        BeanUtils.copyProperties(dto, comment);
        Long commentId = communityService.createComment(comment);
        return Result.ok(commentId);
    }

    /**
     * 删除帖子评论
     */
    @DeleteMapping("/comments/{commentId}")
    public Result deleteComment(@PathVariable(name = "commentId") Long commentId) {
        return Result.ok(communityService.deleteComment(commentId));
    }

    /**
     * 获取帖子评论（默认点赞排序）
     */
    @GetMapping("/posts/{postId}/comments")
    public Result listComments(
            @PathVariable(name = "postId") Long postId,
            @RequestParam(name = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(name = "pageSize", defaultValue = "10") int pageSize,
            @RequestParam(name = "sortBy", defaultValue = "default") String sortBy) {

        return Result.ok(communityService.listCommentsByPost(postId, pageNum, pageSize, sortBy));
    }

    /**
     * 给帖子点赞
     */
    @PostMapping("/posts/{postId}/like")
    public Result likePost(@PathVariable Long postId) {
        return Result.ok(communityService.likePost(postId));
    }

    /**
     * 给评论点赞
     */
    @PostMapping("/comments/{commentId}/like")
    public Result likeComment(@PathVariable Long commentId) {
        return Result.ok(communityService.likeComment(commentId));
    }
}
